package com.example.reference_book.service.impl;

import com.example.reference_book.entity.Attention;
import com.example.reference_book.dao.AttentionDao;
import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.Course;
import com.example.reference_book.service.AttentionService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 关注表(Attention)表服务实现类
 *
 * @author makejava
 * @since 2022-06-07 22:19:03
 */
@Service("attentionService")
public class AttentionServiceImpl implements AttentionService {
    @Resource
    private AttentionDao attentionDao;

    public List<Course> queryAttention(Integer id) {
        return this.attentionDao.queryAttention(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Attention queryById(Integer id) {
        return this.attentionDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param attention 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Attention> queryByPage(Attention attention, PageRequest pageRequest) {
        long total = this.attentionDao.count(attention);
        return new PageImpl<>(this.attentionDao.queryAllByLimit(attention, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param attention 实例对象
     * @return 实例对象
     */
    @Override
    public Attention insert(Attention attention) {
        this.attentionDao.insert(attention);
        return attention;
    }

    /**
     * 修改数据
     *
     * @param attention 实例对象
     * @return 实例对象
     */
    @Override
    public Attention update(Attention attention) {
        this.attentionDao.update(attention);
        return this.queryById(attention.getId());
    }

    public List<Attention> isAttention(Integer id, Integer courseId){
        return this.attentionDao.isAttention(id,courseId);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.attentionDao.deleteById(id) > 0;
    }
    public boolean deleteCourse(Attention attention) {
        return this.attentionDao.deleteCourse(attention) > 0;
    }
}
